<?php

namespace app\user\controller;

use app\department\model\UserModel;
use cmf\controller\AdminBaseController;

class InfoController extends AdminBaseController
{
    public function _initialize()
    {
        $session_admin_id = session('ADMIN_ID');
        if (empty($session_admin_id)) {
            if ($this->request->isPost()) {
                $this->error("您还没有登录！", url("admin/public/login"));
            } else {
                header("Location:" . url("admin/public/login"));
                exit();
            }
        }
    }

    public function setting()
    {
        $id = cmf_get_current_admin_id();
        $userModel = new UserModel();
        if ($this->request->isPost()) {
            $arrData = $this->request->post();;
            $data = [];
            $data['more'] = json_encode($arrData['more']);
            $data['birthday'] = strtotime($arrData['birthday']);
            $data['sex'] = intval($arrData['sex']);
            $data['signature'] = $arrData['signature'];

            if (!empty($arrData['code'])) {
                $data['mobile'] = $arrData['mobile'];
                $errMsg = cmf_check_verification_code($data['mobile'], $arrData['code']);
                if (!empty($errMsg)) {
                    $this->error($errMsg);
                }
            }

            $result = $userModel->validate([
                'mobile' => 'unique:user',
            ], [
                'mobile.unique' => '该手机号已经被绑定！'
            ])->allowField(true)->save($data, [
                'id' => $id
            ]);

            if ($result !== false) {
                $this->success('保存成功');
            } else {
                $this->error($userModel->getError());
            }
        }
        $user = $this->user = $userModel->find($id);
        $user['more'] = json_decode($user['more'], true);

        $this->assign('user', $user);
        return $this->fetch();
    }

    function password()
    {
        if($this->request->isPost()) {
            $userModel = new UserModel();
            $data = $this->request->param();
            if (empty($data['old_password'])) {
                $this->error("原始密码不能为空！");
            }
            if (empty($data['password'])) {
                $this->error("新密码不能为空！");
            }

            $oldPassword = $data['old_password'];
            $password    = $data['password'];
            $rePassword  = $data['re_password'];

            if ($password == $rePassword) {
                $userId = cmf_get_current_admin_id();
                $admin = $userModel->find($userId);
                if (cmf_compare_password($oldPassword, $admin['user_pass'])) {
                    if (cmf_compare_password($password, $admin['user_pass'])) {
                        $this->error("新密码不能和原始密码相同！");
                    } else {
                        $userModel->allowField(true)->save([
                            'user_pass' => $password
                        ],[
                            'id' => $userId
                        ]);
                        $this->success("密码修改成功！");
                    }
                } else {
                    $this->error("原始密码不正确！");
                }
            } else {
                $this->error("密码输入不一致！");
            }

        }
        return $this->fetch();
    }
}
